<template>
  <el-dialog
    :title="!dataForm.paperId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="论文名称" prop="paperName">
      <el-input v-model="dataForm.paperName" placeholder="论文名称"></el-input>
    </el-form-item>
    <el-form-item label="期刊名称" prop="periodicalName">
      <el-input v-model="dataForm.periodicalName" placeholder="期刊名称"></el-input>
    </el-form-item>
    <el-form-item label="发表时间" prop="publishTime">
      <el-input v-model="dataForm.publishTime" placeholder="发表时间"></el-input>
    </el-form-item>
    <el-form-item label="期刊级别" prop="periodicalLevelId">
      <el-input v-model="dataForm.periodicalLevelId" placeholder="期刊级别"></el-input>
    </el-form-item>
    <el-form-item label="索引情况" prop="indexesId">
      <el-input v-model="dataForm.indexesId" placeholder="索引情况"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          paperId: 0,
          paperName: '',
          periodicalName: '',
          publishTime: '',
          periodicalLevelId: '',
          indexesId: ''
        },
        dataRule: {
          paperName: [
            { required: true, message: '论文名称不能为空', trigger: 'blur' }
          ],
          periodicalName: [
            { required: true, message: '期刊名称不能为空', trigger: 'blur' }
          ],
          publishTime: [
            { required: true, message: '发表时间不能为空', trigger: 'blur' }
          ],
          periodicalLevelId: [
            { required: true, message: '期刊级别不能为空', trigger: 'blur' }
          ],
          indexesId: [
            { required: true, message: '索引情况不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.paperId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.paperId) {
            this.$http({
              url: this.$http.adornUrl(`/teacherservice/teacherpaper/info/${this.dataForm.paperId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.paperName = data.teacherPaper.paperName
                this.dataForm.periodicalName = data.teacherPaper.periodicalName
                this.dataForm.publishTime = data.teacherPaper.publishTime
                this.dataForm.periodicalLevelId = data.teacherPaper.periodicalLevelId
                this.dataForm.indexesId = data.teacherPaper.indexesId
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/teacherservice/teacherpaper/${!this.dataForm.paperId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'paperId': this.dataForm.paperId || undefined,
                'paperName': this.dataForm.paperName,
                'periodicalName': this.dataForm.periodicalName,
                'publishTime': this.dataForm.publishTime,
                'periodicalLevelId': this.dataForm.periodicalLevelId,
                'indexesId': this.dataForm.indexesId
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
